package org.zjut.db;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.transaction.annotation.Transactional;
import org.zjut.pso.PSOBase;
import org.zjut.pso.PSOone;
import org.zjut.util.CommonUtil;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author Rising
 * @date 2017/9/20
 * @des
 */
public class LearnerKnowledgeAttrDataBase extends JdbcDaoSupport {
    String dataBase = "pso_recommond";
    String tb_Learner_Knowledge_Attr = ".attr_learner_knowledge";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Autowired
    LearnConceptDataBase mLearnConceptDataBase;
    @Autowired
    ResourceDataBase mResourceDataBase;
    @Autowired
    PSOBase psoBase;

    /**
     * 新增学习者与知识点之间的属性
     * @param params
     * @return
     */
    @Transactional(rollbackFor = java.lang.Exception.class)
    public Map<String,Object> addKnowledgeAttr(Map<String,Object> params) throws Exception{
        String userCode = null , courseCode = null;
        List<Map> conceptInfo = null;
        Map<String,Object> result = new HashMap<>();

        try {
            userCode = params.get("userCode").toString();
            courseCode = params.get("courseCode").toString();
            conceptInfo = JSON.parseArray(params.get("conceptInfo").toString(),Map.class);
        }catch (Exception e){
            return CommonUtil.setErrorInfo("请检查参数");
        }

        String sql = "INSERT INTO "+dataBase+tb_Learner_Knowledge_Attr+"(learnerCode,courseCode,knowledgeCode,attrCode,attrValue,serverTime) " +
                " VALUES(?,?,?,?,?,?)";
        if(conceptInfo!=null&&conceptInfo.size()>0){
            List<Object[]> conditions = new ArrayList<>();
            for(Map<String,Object> conceptItem : conceptInfo){
                String conceptCode = conceptItem.get("conceptCode").toString();
                List<Map> valueInfo = JSON.parseArray(conceptItem.get("valueInfo").toString(),Map.class);
                if (valueInfo!=null&&valueInfo.size()>0){
                    try {
                        for(Map<String,Object> item : valueInfo){
                            Object[] object = new Object[]{userCode,courseCode,conceptCode,
                                    item.get("attrCode").toString(),item.get("attrValue").toString(),sdf.format(new Date())};
                            conditions.add(object);
                        }
                    }catch (Exception e){
                        return CommonUtil.setErrorInfo("请检查参数");
                    }
                }else{
                    return CommonUtil.setErrorInfo("请检查 valueInfo 参数");
                }

            }
            //先删掉原来的关系
            try {
                delKnowledgeAttr(params);
                getJdbcTemplate().batchUpdate(sql,conditions);

                //算法参数可以自己重新定义
                //必须初始化，决定使用哪种推荐算法
                //这里用PSOone算法，只是个demo
                psoBase.setmPSO(new PSOone(mLearnConceptDataBase,mResourceDataBase));
                psoBase.saveRecommondResource(params);
                result.put("success",true);
                result.put("message","添加成功");
            }catch (Exception e){
                System.out.println(e.getMessage()+" 11");
                throw  e;
            }
        }else{
            return CommonUtil.setErrorInfo("请检查参数conceptInfo");
        }

        return result;

    }

    /**
     * 修改学习者与知识点之间的属性
     * @param params
     * @return
     */
    public Map<String,Object> editKnowledgeAttr(Map<String,Object> params){
        return null;
    }

    /**
     * 获取学习者与知识点之间的属性
     * @param params
     * @return
     */
    public Map<String,Object> getKnowledgeAttr(Map<String,Object> params){
        return null;
    }

    /**
     * 删除学习者与知识点之间的属性
     * @param params
     * @return
     */
    public Map<String,Object> delKnowledgeAttr(Map<String,Object> params){
        String userCode = null , courseCode = null ;
        Map<String,Object> result = new HashMap<>();
        try {
            userCode = params.get("userCode").toString();
            courseCode = params.get("courseCode").toString();
        }catch (Exception e){
            return CommonUtil.setErrorInfo("请检查参数");
        }

        String sql = "DELETE FROM "+dataBase+tb_Learner_Knowledge_Attr+" WHERE courseCode = ? AND learnerCode = ? ";
        try {
            getJdbcTemplate().update(sql,new Object[]{courseCode,userCode});
            result.put("success",true);
            result.put("message","删除成功");
        }catch (Exception e){
            System.out.println(e.getMessage()+" 11");
            throw  e;
        }
        return result;
    }
}
